<%--
  Created by IntelliJ IDEA.
  User: 郑希希
  Date: 2025/1/3
  Time: 20:06
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<style>
    table{
        border-collapse: collapse;
        width:80%;
        margin: 0  auto;
    }
    .content {
        margin-left: 210px;
        padding: 20px;
    }
    .table-container {
        background-color: #fff;
        padding: 20px;
        border-radius: 5px;
        box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
    }
    /*table {*/
    /*    width: 100%;*/
    /*    border-collapse: collapse;*/
    /*}*/
    th, td {
        border: 1px solid #ddd;
        padding: 8px;
        text-align: center;
    }
    th {
        background-color: #f2f2f2;
    }
    .page {
        display: flex;
        justify-content: center;
    }
    td:nth-child(3) {
        text-align: left;
    }
</style>
<body>
<jsp:include page="nav.jsp"/>
<h2 style="text-align: center">审核表</h2>
    <table>
        <tbody id="tbody">
        <tr>
            <th>id</th>
            <th>审核类型</th>
            <th>审核备注</th>
            <th>审核对象</th>
            <th>执行的方法</th>
            <th>审核</th>
        </tr>
        </tbody>
        </table>
<div class="page">
    <span>
        <label>页码</label>
        <select id="pages">
            <option value="1">1</option>
        </select>
        <label>页数</label>
        <select id="pageNum">
            <option value="10">10</option>
            <option value="20">20</option>
            <option value="50">50</option>
        </select>
        <label>审核类型</label>
        <select class="searchObj" name="type">
            <option value="">全选</option>
            <option value="0">待审核</option>
            <option value="1">已审核</option>
            <option value="2">为审核</option>
        </select>
        <label>审核对象</label>
        <select class="searchObj" name="objType">
            <option value="">全选</option>
            <option value="0">用户</option>
            <option value="1">楼宇</option>
        </select>
        <label>执行方法</label>
        <select class="searchObj" name="execution">
            <option value="">全选</option>
            <option value="0">删除</option>
            <option value="1">修改</option>
            <option value="2">添加</option>
        </select>
    </span>
</div>
</body>
<script>
    let pages = document.querySelector("#pages");
    let pageNum = document.querySelector("#pageNum");
    let tbody = document.querySelector("#tbody")
    let searchObj = document.querySelectorAll(".searchObj");

    let search = {};

    for (let i = 0; i < searchObj.length; i++) {
        searchObj[i].onchange = function () {
            search[this.name] = this.value;
            getList((Number(pages.value)-1)*Number(pageNum.value), Number(pageNum.value), JSON.stringify(search));
        }
    }


    function getList(pages, pagesNum, selectObj) {
        let ajax = new XMLHttpRequest();
        ajax.open("POST", "/JavaWebStudentManager/check/admin/getlist?pages="+pages+"&pageNum="+pagesNum);
        ajax.setRequestHeader("Content-type", "application/json")

        ajax.onreadystatechange = function () {
            if (ajax.readyState === 4 && ajax.status === 200) {
                let data = JSON.parse(ajax.responseText)['data'];
                let tbodyContent = `<tr>
                                        <th>id</th>
                                        <th>审核类型</th>
                                        <th>审核备注</th>
                                        <th>审核对象</th>
                                        <th>执行的方法</th>
                                        <th>审核</th>
                                    </tr>`
                for (let item of data) {
                    let message = JSON.stringify(JSON.parse(item.message), null, '<br />');
                    tbodyContent += "<tr>" +
                                    "<td>" + item.id+"</td>" +
                                    "<td>" + item.type +"</td>" +
                                    "<td>" + message + "</td>" +
                                    "<td>" + item.objType + "</td>" +
                                    "<td>" + item.execution + "</td>";

                    if (item.type === "待审核") {
                        tbodyContent += "<td>" +
                                            "<button type='button' onclick='doSuccess(" +item.id + ")'>通过</button>" +
                                            "<button type='button' onclick='doError(" +item.id +")'>不通过</button>" +
                                        "</td>"
                    }
                    tbodyContent += "</tr>";
                }
                tbody.innerHTML = tbodyContent;
            }
        }

        ajax.send(selectObj);
    }
    function submitChecked(url, id) {
        let ajax = new XMLHttpRequest();
        ajax.open("GET", "/JavaWebStudentManager/check/admin/"+url+"?id="+id);

        ajax.onreadystatechange = function () {
            if (ajax.readyState === 4 && ajax.status === 200) {
                alert(JSON.parse(ajax.responseText)['message']);
            }
            getList((Number(pages.value)-1)*Number(pageNum.value), Number(pageNum.value), JSON.stringify(search));
        }

        ajax.send();
    }

    function doError(id) {
        submitChecked("filed", id);
    }
    function doSuccess(id) {
        submitChecked("success", id);
    }

    window.onload = function () {
        getList((Number(pages.value)-1)*Number(pageNum.value), Number(pageNum.value), JSON.stringify(search));
    }
</script>
</html>
